package smartgrid.agents.provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import smartgrid.agents.AbstractAgent;
import smartgrid.conf.ProviderConf;
import smartgrid.output.AbstractEntity;
import smartgrid.output.OutputController;
import smartgrid.output.AbstractEntity.EntityType;
import smartgrid.output.ProviderOutputEntity;
import smartgrid.output.ProviderOutputEntity.Field;

public class ProviderAgent extends AbstractAgent implements IProviderAgent {
	
	private static final Logger	logger	= LoggerFactory
																					.getLogger(ProviderAgent.class);
	
	private double							sellPrice;
	
	private double							buyPrice;
	
	private double							qtySellEnergy;
	
	private double							qtyBuyEnergy;
	
	
	/**
	 * Constructor
	 * 
	 * @param id
	 *          Provider identification
	 * @param conf
	 *          Provider configuration
	 * @return none
	 */
	public ProviderAgent(Integer id, ProviderConf conf) {
		super(id);
		this.sellPrice = conf.getSellingPrice();
		this.buyPrice = conf.getPurchasePrice();
		
		logger.debug("[PROVIDER] PRICE SELL " + this.sellPrice);
		logger.debug("[PROVIDER] PRICE BUY " + this.buyPrice);
		
		this.qtySellEnergy = 0.0;
		this.qtyBuyEnergy = 0.0;
	}
	
	
	@Override
	public void initTimestep(int timestep) {
		this.timestep = timestep;
		this.qtySellEnergy = 0.0;
		this.qtyBuyEnergy = 0.0;
	}
	
	
	@Override
	public double sellEnergy(int prosumerId, double qtyEnergy) {	

		double totalPrice = qtyEnergy * this.sellPrice;
		this.qtySellEnergy += qtyEnergy;
		
		return totalPrice;
	}
	
	
	@Override
	public double buyEnergy(int prosumerId, double qtyEnergy) {
		
		double totalPrice = qtyEnergy * this.buyPrice;
		this.qtyBuyEnergy += qtyEnergy;
		
		return totalPrice;
	}
	
	
	@Override
	public void generateOutput() {
		
		AbstractEntity entity = (ProviderOutputEntity) OutputController
				.getEntity(EntityType.PROVIDER);
		
		entity.setValue(Field.CYCLE.name(), this.timestep);
		entity.setValue(Field.QUANTITY_SELL.name(), this.qtySellEnergy);
		entity.setValue(Field.QUANTITY_BUY.name(), this.qtyBuyEnergy);
		entity.setActive();
		
		OutputController.setEntity(EntityType.PROVIDER, entity);
	}
}